//
//  CoreViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/4.
//  Copyright © 2018年 longlong. All rights reserved.
//
#define CardWidth (ScreenWidth - 40.0)
#define CardHeight CardWidth

#import "CoreViewController.h"
#import "iCarousel.h"

@interface CoreViewController ()
<iCarouselDataSource, iCarouselDelegate,UINavigationControllerDelegate>

{
    iCarousel *_cardView;    
}

@property (nonatomic ,strong) UIImageView *backImageView;

@property (nonatomic ,strong) NSArray<NSString *> *titleArray;
@property (nonatomic ,strong) NSArray<NSString *> *controllerArray;
@end

@implementation CoreViewController

- (void)dealloc
{
    _cardView.delegate = nil;
    _cardView.dataSource = nil;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self.view addSubview:self.backImageView];
    self.view.backgroundColor = COLOR_RGBA(235, 236, 236,1);
    self.navigationController.navigationBar.hidden = YES;
    
    _cardView = [[iCarousel alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight - 49 - 30)];
    _cardView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_cardView];
    
    _cardView.clipsToBounds = YES;
    _cardView.delegate = self;
    _cardView.dataSource = self;
    _cardView.type = iCarouselTypeCoverFlow;
    _cardView.vertical = YES;
    _cardView.bounces = NO;
    _cardView.scrollToItemBoundary = YES;
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 判断要显示的控制器是否是自己
    BOOL isSelf = [viewController isKindOfClass:[CoreViewController class]];
    if (isSelf)
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    else
    {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel
    return [self.titleArray count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    UIImageView *imageView = nil;
    UIButton *button = nil;
    
    CGFloat lableHeight = 40.0;
    
    if (view == nil)
    {
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CardWidth, CardHeight + lableHeight)];
        view.clipsToBounds = YES;
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CardWidth, CardHeight)];
        imageView.tag = 2;
        imageView.clipsToBounds = YES;
        //        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imageView];
        
        UIImageView *bottomImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CardHeight - 1, CardWidth, lableHeight + 2)];
        bottomImageView.image = [UIImage imageNamed:@"navBar"];
        [view addSubview:bottomImageView];
        //        view.contentMode = UIViewContentModeCenter;
        
        label = [[UILabel alloc] initWithFrame:CGRectMake(10, CardHeight, CardWidth - 20, lableHeight)];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentRight;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        label.tag = 1;
        [view addSubview:label];
        
        
        
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(brandButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = view.bounds;
        button.tag = 3;
        [view addSubview:button];
        
    }
    else
    {
        label = (UILabel *)[view viewWithTag:1];
        imageView = (UIImageView *)[view viewWithTag:2];
        button = (UIButton *)[view viewWithTag:3];
    }
    
    
    
    if (_titleArray)
    {
        label.text = _titleArray[index];
        [button setTitle:label.text forState:UIControlStateNormal];
        imageView.image = [UIImage imageNamed:@"register_Default"];
    }
    
    return view;
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    if (option == iCarouselOptionSpacing)
    {
        return value * 1.1;
    }
    else if (option == iCarouselOptionWrap)
    {
        return 1;
    }
    /*
     
     case iCarouselOptionTilt:
     {
     return _tiltSlider.value;
     }
     case iCarouselOptionSpacing:
     {
     return value * _spacingSlider.value;
     }
     */
    return value;
}

- (void)brandButtonClick:(UIButton *)button
{
    NSInteger index = [self.titleArray indexOfObject:button.titleLabel.text];
    NSString *className = self.controllerArray[index];
    UIViewController *vc = (UIViewController *)[[NSClassFromString(className) alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getters And Setters

- (UIImageView *)backImageView
{
    if (_backImageView == nil)
    {
        _backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"brandBack"]];
        _backImageView.alpha = .9f;
        _backImageView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - 49);
    }
    
    return _backImageView;
}

//注解：OC 中我们很习惯的会用懒加载，当用到的时候才去加载它，但是实际上只要一个类实现了某个方法，就会被加载进内存。
- (NSArray<NSString *> *)titleArray
{
    if (_titleArray == nil)
    {
        _titleArray = @[@"关键字",@"Runtime",@"Runloop",@"消息转发",@"Method",@"Class",@"isKindOfClass",@"Block",@"线程管理",@"NSURLSession"];
    }
    
    return _titleArray;
}

- (NSArray<NSString *> *)controllerArray
{
    if (_controllerArray == nil)
    {
        _controllerArray = @[@"KeywordViewController",@"RunTimeViewController",
                             @"RunloopViewController",@"MessageViewController",
                             @"MethodViewController",@"ClassViewController",
                             @"IsKindClassViewController",@"BlockViewController",
                             @"ThreadManagerViewController",@"URLSessionTableViewController"];
    }
    
    return _controllerArray;
}


@end
